package com.ikangtai.shecare.curve.mpchart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.drawable.Drawable;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineScatterCandleRadarDataSet;
import com.github.mikephil.charting.renderer.LineChartRenderer;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.ikangtai.shecare.R;


public class BBTLineChartRenderer extends LineChartRenderer {
    private Context context;
    private Path mHighlightLinePath = new Path();
    private static float WIDTH = 0;

    private Drawable fade_menses_drawable;
    private Drawable fade_ovulation_drawable;
    private Drawable fade_other_drawable;

    public BBTLineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler, Context context) {
        super(chart, animator, viewPortHandler);
        this.context = context;

        fade_menses_drawable = context.getResources().getDrawable(R.drawable.fade_menses);
        fade_ovulation_drawable = context.getResources().getDrawable(R.drawable.fade_ovulation);
        fade_other_drawable = context.getResources().getDrawable(R.drawable.fade_other);
    }

    @Override
    protected void drawHighlightLines(Canvas c, float x, float y, ILineScatterCandleRadarDataSet set) {
        // set color and stroke-width
        mHighlightPaint.setColor(set.getHighLightColor());
        mHighlightPaint.setStrokeWidth(set.getHighlightLineWidth());

        // draw highlighted lines (if enabled)
        mHighlightPaint.setPathEffect(set.getDashPathEffectHighlight());

        // draw vertical highlight lines
        if (set.isVerticalHighlightIndicatorEnabled()) {

            // create vertical path
            mHighlightLinePath.reset();
            mHighlightLinePath.moveTo(x, y);
            mHighlightLinePath.lineTo(x, mViewPortHandler.contentBottom());

            c.drawPath(mHighlightLinePath, mHighlightPaint);
        }

        // draw horizontal highlight lines
        if (set.isHorizontalHighlightIndicatorEnabled()) {

            // create horizontal path
            mHighlightLinePath.reset();
            mHighlightLinePath.moveTo(mViewPortHandler.contentLeft(), y);
            mHighlightLinePath.lineTo(mViewPortHandler.contentRight(), y);

            c.drawPath(mHighlightLinePath, mHighlightPaint);
        }
    }
//
//    @Override
//    protected void drawLinearFill(Canvas c, ILineDataSet dataSet, Transformer trans, XBounds bounds) {
//
//        final Path filled = mGenerateFilledPathBuffer;
//
//        final int startingIndex = bounds.min;
//        final int endingIndex = bounds.range + bounds.min;
//        final int indexInterval = 128;
//
//        int currentStartIndex = 0;
//        int currentEndIndex = indexInterval;
//        int iterations = 0;
//
//        // Doing this iteratively in order to avoid OutOfMemory errors that can happen on large bounds sets.
//        do {
//            currentStartIndex = startingIndex + (iterations * indexInterval);
//            currentEndIndex = currentStartIndex + indexInterval;
//            currentEndIndex = currentEndIndex > endingIndex ? endingIndex : currentEndIndex;
//
//            if (currentStartIndex <= currentEndIndex) {
//                final Drawable drawable = dataSet.getFillDrawable();
//                generateFilledPath(dataSet, currentStartIndex, currentEndIndex, filled, drawable, c);
//
//                trans.pathValueToPixel(filled);
//
//                if (drawable != null) {
//
//                    drawFilledPath(c, filled, drawable);
//                } else {
//
//                    drawFilledPath(c, filled, dataSet.getFillColor(), dataSet.getFillAlpha());
//                }
//            }
//
//            iterations++;
//
//        } while (currentStartIndex <= currentEndIndex);
//
//    }
//
//    private void generateFilledPath(final ILineDataSet dataSet, final int startIndex, final int endIndex, final Path outputPath, Drawable drawable, Canvas c) {
//
//        final float fillMin = dataSet.getFillFormatter().getFillLinePosition(dataSet, mChart);
//        final float phaseY = mAnimator.getPhaseY();
//        final boolean isDrawSteppedEnabled = dataSet.getMode() == LineDataSet.Mode.STEPPED;
//
//        final Path filled = outputPath;
//        filled.reset();
//
//        final Entry entry = dataSet.getEntryForIndex(startIndex);
//
//        filled.moveTo(entry.getX(), fillMin);
//        filled.lineTo(entry.getX(), entry.getY() * phaseY);
//
//        // create a new path
//        Entry currentEntry = null;
//        Entry previousEntry = null;
//        for (int x = startIndex + 1; x <= endIndex; x++) {
//
//            currentEntry = dataSet.getEntryForIndex(x);
//
//            if (isDrawSteppedEnabled && previousEntry != null) {
//                filled.lineTo(currentEntry.getX(), previousEntry.getY() * phaseY);
//            }
//
//            filled.lineTo(currentEntry.getX(), currentEntry.getY() * phaseY);
//
//            previousEntry = currentEntry;
//        }
//
//        // close up
//        if (currentEntry != null) {
//            filled.lineTo(currentEntry.getX(), fillMin);
//
//            Paint paint = new Paint();
//            paint.setColor(Color.BLACK);
//            paint.setTextSize(30);
//            paint.setTextAlign(Paint.Align.CENTER);
//
//            // hOffset是离下边即X轴的距离，vOffset是离左边垂直边的距离
//            c.drawTextOnPath(getPeriodStr(drawable), filled, 50, -20, paint);
//        }
//
//        filled.close();
//    }
//
//    private String getPeriodStr(Drawable drawable){
//        String periodStr = "安全期";
//
//        if(drawable == context.getResources().getDrawable(R.drawable.fade_menses))
//            periodStr = "月经期";
//        else if(drawable == context.getResources().getDrawable(R.drawable.fade_ovulation))
//            periodStr = "易孕期";
//
//        return periodStr;
//    }
}
